package com.yu.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.yu.model.dto.question.JudgeCase;
import com.yu.model.dto.question.JudgeConfig;
import com.yu.judge.model.JudgeInfo;
import com.yu.model.entity.Question;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class DefaultJudgeStrategy implements JudgeStrategy {

    /**
     * 执行判题
     * ----
     * 目前为止为测试阶段  后面应该补充执行信息给response 的messaage值作为结果返回
     *
     * @param judgeContext
     * @return
     */
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {


        List<String> output = judgeContext.getOutput();

        List<String> input = judgeContext.getInput();

        Question question = judgeContext.getQuestion();

        List<JudgeCase> inputList = judgeContext.getInputList();

        String judgeConfig = question.getJudgeConfig();
        //转换成json数组
        JudgeConfig config = JSONUtil.toBean(judgeConfig, JudgeConfig.class);
        Long memory = config.getMemoryLimit();
        Long timeLimit = config.getTimeLimit();
        Long stackLimit = config.getStackLimit();


        JudgeInfo judgeInfoResponse = judgeContext.getJudgeInfo();
        Long judgeInfoTime = judgeInfoResponse.getTime();
        Long judgeInfoMemory = judgeInfoResponse.getMemory();
        String message = judgeInfoResponse.getMessage();
        Long judgeInfoStackLimit = judgeInfoResponse.getStackLimit();


        String workInfo = judgeContext.getMessage();

//        JudgeInfo judgeInfoResponse = new JudgeInfo();
//        judgeInfoResponse.setTime(judgeInfoTime);
//        judgeInfoResponse.setMemory(judgeInfoMemory);
//        judgeInfoResponse.setMessage("正确");
//        judgeInfoResponse.setStackLimit(judgeInfoStackLimit);

        if (workInfo.equals("编译失败")) {
            return judgeInfoResponse;
        }

        if (message.equals("运行成功")) {
            if (output.size() != input.size()) {
                return judgeInfoResponse;
            }
            for (int i = 0; i < inputList.size(); i++) {
                JudgeCase judgeCase = inputList.get(i);
                System.out.println(judgeCase.getOutput());
                System.out.println(output.get(i));
                if (!judgeCase.getOutput().equals(output.get(i))) {
//                log.info("有示例不符合预期");
                    judgeInfoResponse.setMessage("结果错误");
                    return judgeInfoResponse;
                }
            }
            //获取题目的config

            if (memory < judgeInfoMemory) {
//            log.info("内存不符合预期");
                judgeInfoResponse.setMessage("内存不符合预期");
                return judgeInfoResponse;
            }
            if (timeLimit < judgeInfoTime) {
//            log.info("时间限制不符合预期");
                judgeInfoResponse.setMessage("时间限制不符合预期");
                return judgeInfoResponse;
            }
            if (stackLimit < judgeInfoStackLimit) {

//            log.info("栈内存不符合预期");
                judgeInfoResponse.setMessage("栈内存不符合预期");
                return judgeInfoResponse;
            }
        }

        return judgeInfoResponse;
    }
}
